<?php
include_once './core/Modelo.php';
/**
 * Operaciones con la tabla almacen
 *
 * @author Jose
 */
class Almacen extends Modelo
{
    
    private $_tablename;
    private $_datetime;
    private $_fechaHoy;
    private $_fechaAyer;
    
    public function __construct( DateTime $dateTime)
    {
        parent::__construct();
        $this->_datetime = $dateTime;
        $this->_fechaHoy = $dateTime->format('Y-m-d');
        $dateTime->modify('-1 day');
        $this->_fechaAyer = $dateTime->format('Y-m-d');        
        $this->_tablename = 'almacen';
    }

    
    public function setCerrado()
    {
        
        $return = array();
        $sql = "SELECT DiaInicio, DiaFinal, DiaFinalInscripcion, AlmacenCod, Estado                
                FROM   $this->_tablename
                WHERE  DiaFinal = '$this->_fechaAyer'
                AND estado != 'Cerrado'";
        $results = $this->fetchAll($sql);

        if( !empty( $results ) ){    
            foreach ( $results as $result ) {
                $update = $this->update( $this->_tablename, array('Estado' => 'Cerrado'), array( 'AlmacenCod' => $result->AlmacenCod ) );       
                if( $update ){
                    $date = date('Y-m-d H:i:s');
                    $return[] = ">> [ $date ] AlmacenCod: $result->AlmacenCod - Cerrado< /br>\n";
                }
            }    
        }
        return $return;
        
    }
    
    public function setDictando()
    {       

        $return = array();
        $sql = "SELECT  DiaInicio, DiaFinal, DiaFinalInscripcion, AlmacenCod, Estado
                FROM    $this->_tablename
                WHERE   DiaInicio = '$this->_fechaHoy'
                AND estado = 'Abierto'";
        $results = $this->fetchAll($sql);

        if( !empty( $results ) ){    
            foreach ( $results as $result ) {
                $update = $this->update( $this->_tablename, array('Estado' => 'Dictando'), array( 'AlmacenCod' => $result->AlmacenCod ) );       
                if( $update ){
                    $date = date('Y-m-d H:i:s');
                    $return[] = ">> [ $date ] AlmacenCod: $result->AlmacenCod  - Dictando< /br>\n";
                }
            }    
        }
        return $return;
        
    }
    
    
}
